package com.framework.common.config;

import java.io.UnsupportedEncodingException;
import java.util.*;

import com.framework.common.utils.QuickHelper;
import com.framework.modules.sys.entity.SysConfigEntity;
import com.framework.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.framework.modules.sys.shiro.ShiroTag;

/**
 * Freemarker配置
 */
@Configuration
public class FreemarkerConfig {

	@Autowired
	private SysConfigService sysConfigService;

	@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer(ShiroTag shiroTag) throws UnsupportedEncodingException {
		FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
		configurer.setTemplateLoaderPath("classpath:/templates");

		Map<String, Object> variables = new HashMap<>(1);
		variables.put("shiro", shiroTag);
		variables.put("guid",java.util.UUID.randomUUID());
		variables.put(QuickHelper.base64decode("cGxhdGZvcm0="), QuickHelper.base64decode("5Y+L6KGX55u05pKt6L+Q57u0566h55CG5bmz5Y+wIHYxLjA="));

		Map<String, Object> map = new HashMap<>();
		List<SysConfigEntity> list = sysConfigService.queryAll();
		for(SysConfigEntity entry : list){
			String val = entry.getParamValue();
			map.put(entry.getParamKey(),val);
		}
		variables.put("sys",map);

		configurer.setFreemarkerVariables(variables);

		Properties settings = new Properties();
		settings.setProperty("default_encoding", "utf-8");
		settings.setProperty("number_format", "0.##");
		configurer.setFreemarkerSettings(settings);


		return configurer;
	}

}
